package com.zhuzi.springmvcpro.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhuzi.springmvcpro.dao.User;
import com.zhuzi.springmvcpro.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author : zga
 * @date : 2016-10-14
 *
 * 页面控制器类
 */
@Controller
@RequestMapping("/page")
public class PageController {

    @Resource
    UserService userService;

    @RequestMapping("/userList")
    public String getUser (Model model) {
        User user = new User();
        user.setUser_name("zhuzi");
        user.setPassword("123456");
        user.setBirthday(new Date());

        model.addAttribute("user", user);
        return "user";
    }

    @RequestMapping(value = "/saveUser",method = RequestMethod.GET)
    public String saveUser(){
        return "saveUser";
    }

    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject saveUser (HttpServletRequest request, User user) {
        JSONObject resultJson = new JSONObject();

        /**
         * 这里输出user对象后,可以发现birthday是日期对象
         * */
        System.out.println(user);


        return resultJson;
    }
}
